package pro.jiug.kExcel.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 图片上传工具类
 * Created by charlin on 2017/9/10.
 */
public class UploadFile {


    /**
     * @param file 上传的文件
     * @param path 文件的虚拟路径，例如 /upload
     * @param fileLocation 文件的物理路径，例如d:/upload
     * @return 文件的虚拟路径+文件名
     *
     * fileName.lastIndexOf(".")为获取后缀名
     * UUID.randomUUID()为获取唯一标识，保证文件的唯一性
     */

    public static String upload(MultipartFile file, String path, String fileLocation) {
        String fileFinishName = null;
        try {
            // 如果目录不存在则创建
            File uploadDir = new File(fileLocation);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }
            //获取源文件名称
            String fileName = file.getOriginalFilename();
            System.out.println(fileName);
            fileFinishName = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
            //上传文件到指定目录下
            File uploadFile = new File(uploadDir + uploadDir.separator + fileFinishName);
            file.transferTo(uploadFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return path + "/" + fileFinishName;
    }

    public static String upload(MultipartFile file, String fileLocation) {
        String fileFinishName = null;
        try {
            // 如果目录不存在则创建
            File uploadDir = new File(fileLocation);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }
            //获取源文件名称
            String fileName = file.getOriginalFilename();
            System.out.println(fileName);
            fileFinishName = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
            //上传文件到指定目录下
            File uploadFile = new File(uploadDir + uploadDir.separator + fileFinishName);
            file.transferTo(uploadFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return fileLocation + "/" + fileFinishName;
    }

    // 删除文件或文件夹以及文件夹下所有文件
    public static void delDir(String dirPath) throws IOException {
        System.out.println("删除文件开始:{"+dirPath+"}.");
        long start = System.currentTimeMillis();
        try{
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                return;
            }
            if (dirFile.isFile()) {
                dirFile.delete();
                return;
            }
            File[] files = dirFile.listFiles();
            if(files==null){
                return;
            }
            for (int i = 0; i < files.length; i++) {
                delDir(files[i].toString());
            }
            dirFile.delete();
            System.out.println("删除文件:{"+dirPath+"}. 耗时:{"+(System.currentTimeMillis()-start)+"}ms. ");
        }catch(Exception e){
            System.out.println("删除文件:{"+dirPath+"}. 异常:{"+e+"}. 耗时:{"+(System.currentTimeMillis()-start)+"}ms. ");
            throw new IOException("删除文件异常.");
        }
    }
}
